package com.pybox.spec;

import com.pybox.entity.TCabinets;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
* 描述：TCabinets Specification
* @author wuyingkai
* @date 2018-09-15
*/


public class TCabinetsSpec implements Specification<TCabinets> {

    private Map<String, String> params;

    public TCabinetsSpec(Map<String, String> params){
        this.params = params;
    }

    @Override
    public Predicate toPredicate(Root<TCabinets> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {


        String uuid = this.params.get("uuid");
        String customerId = this.params.get("customerId");
        String name = this.params.get("name");
        String position = this.params.get("position");
        String timezone = this.params.get("timezone");
        String ip = this.params.get("ip");
        String port = this.params.get("port");
        String passwd = this.params.get("passwd");
        String timeout = this.params.get("timeout");
        String openTimeout = this.params.get("openTimeout");
        String comments = this.params.get("comments");
        String deleted = this.params.get("deleted");
        String version = this.params.get("version");
        String ctime = this.params.get("ctime");
        String creatUserid = this.params.get("creatUserid");
        String createUsername = this.params.get("createUsername");
        String utime = this.params.get("utime");
        String lastUpdate = this.params.get("lastUpdate");
        String isOnline = this.params.get("isOnline");
        String doorStatus = this.params.get("doorStatus");
        List<Predicate> ps = new ArrayList<>();


        if (!StringUtils.isEmpty(uuid)) {
            ps.add(criteriaBuilder.like(root.get("uuid"), "%" + uuid + "%"));
        }
        if (!StringUtils.isEmpty(customerId)) {
            ps.add(criteriaBuilder.equal(root.get("customerId"), customerId));
        }
        if (!StringUtils.isEmpty(name)) {
            ps.add(criteriaBuilder.like(root.get("name"), "%" + name + "%"));
        }
        if (!StringUtils.isEmpty(position)) {
            ps.add(criteriaBuilder.like(root.get("position"), "%" + position + "%"));
        }
        if (!StringUtils.isEmpty(timezone)) {
            ps.add(criteriaBuilder.like(root.get("timezone"), "%" + timezone + "%"));
        }
        if (!StringUtils.isEmpty(ip)) {
            ps.add(criteriaBuilder.like(root.get("ip"), "%" + ip + "%"));
        }
        if (!StringUtils.isEmpty(port)) {
            ps.add(criteriaBuilder.like(root.get("port"), "%" + port + "%"));
        }
        if (!StringUtils.isEmpty(passwd)) {
            ps.add(criteriaBuilder.like(root.get("passwd"), "%" + passwd + "%"));
        }
        if (!StringUtils.isEmpty(timeout)) {
            ps.add(criteriaBuilder.like(root.get("timeout"), "%" + timeout + "%"));
        }
        if (!StringUtils.isEmpty(openTimeout)) {
            ps.add(criteriaBuilder.like(root.get("openTimeout"), "%" + openTimeout + "%"));
        }
        if (!StringUtils.isEmpty(comments)) {
            ps.add(criteriaBuilder.like(root.get("comments"), "%" + comments + "%"));
        }
        if (!StringUtils.isEmpty(deleted)) {
            ps.add(criteriaBuilder.like(root.get("deleted"), "%" + deleted + "%"));
        }
        if (!StringUtils.isEmpty(version)) {
            ps.add(criteriaBuilder.like(root.get("version"), "%" + version + "%"));
        }
        if (!StringUtils.isEmpty(ctime)) {
            ps.add(criteriaBuilder.like(root.get("ctime"), "%" + ctime + "%"));
        }
        if (!StringUtils.isEmpty(creatUserid)) {
            ps.add(criteriaBuilder.like(root.get("creatUserid"), "%" + creatUserid + "%"));
        }
        if (!StringUtils.isEmpty(createUsername)) {
            ps.add(criteriaBuilder.like(root.get("createUsername"), "%" + createUsername + "%"));
        }
        if (!StringUtils.isEmpty(utime)) {
            ps.add(criteriaBuilder.like(root.get("utime"), "%" + utime + "%"));
        }
        if (!StringUtils.isEmpty(lastUpdate)) {
            ps.add(criteriaBuilder.like(root.get("lastUpdate"), "%" + lastUpdate + "%"));
        }
        if (!StringUtils.isEmpty(1)) {
            ps.add(criteriaBuilder.like(root.get("isOnline"), "%" + isOnline + "%"));
        }
        if (!StringUtils.isEmpty(doorStatus)) {
            ps.add(criteriaBuilder.equal(root.get("doorStatus"),doorStatus ));
        }
        criteriaQuery = criteriaQuery.where(ps.toArray(new Predicate[ps.size()]));

        return criteriaQuery.getRestriction();
    }

}